﻿$(function () {
    var width = $(window).width() * 0.95;
    if (width < 1000)
        width = 1000;
    width = Math.ceil(width / $(window).width() * 100) + '%';


    $('label[for=regulations] a').fancybox({
        'width': width,
        'height': '95%',
        'autoScale': false,
        'transitionIn': 'fadeIn',
        'transitionOut': 'none',
        'type': 'iframe'
    });
    $('.page-content').wrap('<form id="progress" method="POST" />');
    $('#progress').validationEngine();
    $('.page-content .gg-b2').addClass('submittrigger');

    $('.page-content .gg-b2').css('cursor', 'pointer').click(function () {
        if ($('#progress').validationEngine('validate')) {
            $('#progress').ajaxSubmit({
                url: '/Handlers/ApartsService.asmx/AddReservation',
                beforeSubmit: beforeSubmit,
                success: showResponse
            });
        }
    });

    $('#hidVariantId').attr('name', 'hidVariantId');
    $('#hidReservationId').attr('name', 'hidReservationId');
    $('#ddlPersonHour').attr('name', 'ddlPersonHour');
});

function beforeSubmit() {
    $('.pres').css('visibility', 'hidden');
    $('.beforeSend').show();
    disableForm();
}

var rs;

function showResponse(responseText, statusText, xhr, $form) {
    var number = $(responseText).find('string').text();
    $('.afterAdd .red').text(number);
    $('.afterAdd .red').attr('href', $('.afterAdd .red').attr('href').replace('reservation-number', number));
    $('.beforeSend').hide();
    $('.afterAdd').show();
}

function disableForm() {
    $('.page-content input[type=text]').each(function () {
        $(this).replaceWith('<span>' + $(this).val() + '</span>');
    });
    $('.page-content select:visible').each(function () {
        $(this).replaceWith('<span>' + $(this).find('option[value="' + $(this).val() + '"]').text() + '</span>');
    });
    $('.page-content input[type=checkbox], .page-content input[type=radio]').each(function () {
        $(this).attr('disabled', true);
    });
}
